package cn.wolfcode.trip.admin.controller;

import cn.wolfcode.trip.base.domain.Ticket;
import cn.wolfcode.trip.base.query.QueryObject;
import cn.wolfcode.trip.base.service.ITicketService;
import cn.wolfcode.trip.base.util.UploadUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

@Controller
@RequestMapping("ticketes")
public class TicketController {
    @Autowired
    private ITicketService ticketService;

    @RequestMapping("list")
    public String listAll(@ModelAttribute("qo") QueryObject qo, Model model) {
        model.addAttribute("pageInfo", ticketService.query(qo));
        return "/alltickets/ticket";
    }

    @RequestMapping("save")
    public String saveOrUpdate(MultipartFile file, Ticket ticket) {
        if (file != null && file.getSize() > 0) {
            String upload = UploadUtil.uploadQiniu(file);
            ticket.setPictureImg(UploadUtil.QINIU_PATH + "/" + upload);
        }
        ticketService.saveOrUpdate(ticket);
        return "redirect:/ticketes/list.do";
    }

    /**
     * 删除景点
     *
     * @return
     */
    @RequestMapping("delete")
    @ResponseBody
    public String delete(Long id) {
        ticketService.deleteTicket(id);
        return "/ticketes/list.do";
    }


}
